package com.chuangjiangx.member.basic.ddd.query;

import com.chuangjiangx.member.basic.ddd.dal.dto.*;
import com.chuangjiangx.member.stored.ddd.dal.condition.AppOrderCountCondition;
import com.chuangjiangx.member.basic.ddd.dal.mapper.MbrCountDalMapper;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 会员分析query
 * @author cj-yzh
 * @date 2018/8/7 下午4:00
 */

@Component
public class MbrAnalysisQuery {


    private Integer[] range;

    @Autowired
    private MbrCountDalMapper mbrCountDalMapper;

    //查询总数，新增会员数量
    public MbrCount queryCount(AppOrderCountCondition condition){
        MbrCount members = mbrCountDalMapper.appCountMembers(condition);

        return members;
    }

    //按终端查询
    public List<TerminalScales> queryByTiminal(AppOrderCountCondition condition){

        List<TerminalScales> terminalScales = mbrCountDalMapper.appCountRegistoryTerminalScale(condition);
        return terminalScales;
    }

    //按照性别查询
    public List<SexScales> queryBySex(AppOrderCountCondition condition){
        List<SexScales> sexScalesList = mbrCountDalMapper.appCountSexScale(condition);
        //获得性别分组会员
        List<AppSexGroup> appSexGroups = mbrCountDalMapper.appSexMemberGroup(condition);

        if(sexScalesList == null || sexScalesList.size() == 0){
            SexScales manScale = new SexScales();
            manScale.setSex(0);
            manScale.setNewMembers(0);
            sexScalesList.add(manScale);
            SexScales womanScale = new SexScales();
            womanScale.setSex(1);
            womanScale.setNewMembers(0);
            sexScalesList.add(womanScale);
        }
        if(sexScalesList.size() == 1){
            SexScales sexScale = new SexScales();
           if(sexScalesList.get(0).getSex() == 1){
               sexScale.setSex(0);
           }else{
               sexScale.setSex(1);
           }
           sexScale.setNewMembers(0);
           sexScalesList.add(sexScale);
        }
        Map<Integer , Integer> map = new HashMap<>();
        if(appSexGroups != null){
            for(int i=0 ; i<appSexGroups.size();i++){
                map.put(appSexGroups.get(i).getSex(),appSexGroups.get(i).getMemberNum());
            }
        }
        for(int i=0 ; i<2 ; i++){
            if(map.get(sexScalesList.get(i).getSex()) == null){
                sexScalesList.get(i).setTotalMembers(0);
            }else {
                sexScalesList.get(i).setTotalMembers(map.get(sexScalesList.get(i).getSex()));
            }

        }
        return sexScalesList;
    }
    //按照门店查询
    public List<StoreScales> queryByStore(AppOrderCountCondition condition){
        List<StoreScales> storeScales = mbrCountDalMapper.appCountStoreScale(condition);
        return storeScales;
    }
    //按照时间查询
    public List<TimeScales> queryByTime(AppOrderCountCondition condition){
        Date end = condition.getEnd();
        Date start = condition.getStart();
        //求相差得天数
        int days = (int) ((end.getTime() - start.getTime()) / (1000*3600*24));
        //获得总天数
            MbrCount members = mbrCountDalMapper.appCountMembers(condition);
        List<TimeScales> timeScaless = null;
        List<String> dateRange = null;
        if(days > 1){
            dateRange = getTimeRangeByDay(start, end);
            timeScaless = mbrCountDalMapper.appCountTimeScaleByDay(condition);
        }else{
            dateRange = getTimeRangeByHour(start, end);
            timeScaless = mbrCountDalMapper.appCountTimeScaleByHour(condition);
        }
        Map<String,TimeScales> temp = new HashedMap();
        //设置总数
        for(TimeScales timeScale : timeScaless){
            timeScale.setTotalMembers(members.getTotalMembers());
            temp.put(timeScale.getTimeStr(),timeScale);
        }
        List<TimeScales> timeScales = new ArrayList<>();
        for (String s : dateRange) {
            if(temp.get(s) == null){
                TimeScales newNode = new TimeScales();
                newNode.setTimeStr(s);
                newNode.setNewMembers(0);
                newNode.setTotalMembers(members.getTotalMembers());
                timeScales.add(newNode);
            }else{
                timeScales.add(temp.get(s));
            }
        }

        return timeScales;
    }


    //按照时间范围返回按天分组
    private List<String> getTimeRangeByDay(Date start , Date end){
        List<String> dateRange = new ArrayList<String>();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        sdf1.format(start);
        dateRange.add(sdf1.format(start));
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(start);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(end);
        // 测试此日期是否在指定日期之后
        while (end.after(calBegin.getTime()))  {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            sdf1.format(calBegin.getTime());
            dateRange.add(sdf1.format(calBegin.getTime()));
        }
        dateRange.remove(dateRange.size()-1);
        return dateRange;
    }
    //按照时间范围返回按小时分组
    private List<String> getTimeRangeByHour(Date start , Date end){
        List<String> dateRange = new ArrayList<String>();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHH");
        sdf1.format(start);
        dateRange.add(sdf1.format(start));
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(start);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(end);
        // 测试此日期是否在指定日期之后
        while (end.after(calBegin.getTime()))  {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.HOUR_OF_DAY, 1);
            sdf1.format(calBegin.getTime());
            dateRange.add(sdf1.format(calBegin.getTime()));
        }
        return dateRange;
    }


    //会员活跃分析
    public List<Actives> queryByScope(AppOrderCountCondition condition){
        List<Actives> mbrActiveScales = mbrCountDalMapper.appCountMbrActiveScale(condition);
        //前端要求按顺序返回
        Map<String,Integer> map = new HashMap();
        for (Actives mbrActiveScale : mbrActiveScales) {
            map.put(mbrActiveScale.getName(),mbrActiveScale.getCount());
        }
        List<Actives> result = new ArrayList<>();
        result.add(new Actives("无消费"));
        result.add(new Actives("消费1次"));
        result.add(new Actives("消费2-4次"));
        result.add(new Actives("消费5-9次"));
        result.add(new Actives("消费10-14次"));
        result.add(new Actives("消费15次以上"));
        for (int i=0 ; i<result.size(); i++) {
            if(map.get(result.get(i).getName()) == null){
                result.remove(i);
                i--;
            }else{
                result.get(i).setCount(map.get(result.get(i).getName()));
            }
        }
        return result;
    }
}
